package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.apiservice.XxOrderApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityXxOrderDetailBinding;
import com.ycrlkj.android.modulescore.main.entity.XxOrder;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_XX_ORDER_DETAIL)
public class XxOrderDetailActivity extends AppCompatActivity {

    @Autowired
    String id;

    @Inject
    XxOrderApiService xxOrderApiService;

    private ActivityXxOrderDetailBinding binding;

    private XxOrder xxOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);

        if (id == null || id.length() != 19) {
            XPopupUtil.showAlert(this, "未找到订单信息, 关闭页面", new OnConfirmListener() {
                @Override
                public void onConfirm() {
                    XxOrderDetailActivity.this.finish();
                }
            });
        }

        binding = ActivityXxOrderDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initTitleBar();
        get(id);
    }

    @SuppressLint("CheckResult")
    public void get(String id) {
        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);
        xxOrderApiService.getById(id)
                .compose(ResponseTransformer.obtain())
                .subscribe(xxOrder -> {
                    binding.setXxOrder(xxOrder);
                    this.xxOrder = xxOrder;
                    XPopupUtil.hideLoading(loadingPopupView);
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        XPopupUtil.hideLoading(loadingPopupView);
                        ApiExceptionHandler.doWith(XxOrderDetailActivity.this, e);
                    }
                });
    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                XxOrderDetailActivity.this.finish();
            }

            @Override
            public void setBtnRightOnClickListener() {

            }
        });
    }

    public void handleToXxFreightOrderDetail(View view) {
        String freightOrderId = xxOrder.getFreightOrderId();
        if(TextUtils.isEmpty(freightOrderId)){
            ToastUtils.showLong("未找到货运订单信息");
            return;
        }

        ARouter.getInstance().build(RoutePath.PAGE_FREIGHT_ORDER_DETAIL).withString("id", freightOrderId).navigation();
    }

    public void handleToVehicleDetail(View view) {
        String vehicleId = xxOrder.getVehicleId();
        if(TextUtils.isEmpty(vehicleId)){
            ToastUtils.showLong("未找车辆信息");
            return;
        }
        ARouter.getInstance().build(RoutePath.PAGE_VEHICLE_DETAIL).withString("id", vehicleId).navigation();
    }

    public void handleToXxStationDetail(View view) {
        String xxStationId = xxOrder.getXxStationId();
        if(TextUtils.isEmpty(xxStationId)){
            ToastUtils.showLong("未找洗消站点信息");
            return;
        }
        ARouter.getInstance().build(RoutePath.PAGE_XX_STATION_DETAIL).withString("id", xxStationId).navigation();
    }

    public void handleToReportDetail(View view) {
        String xxReportId = xxOrder.getXxReportId();
        if(TextUtils.isEmpty(xxReportId)){
            ToastUtils.showLong("未找洗消报告信息");
            return;
        }
        ARouter.getInstance().build(RoutePath.PAGE_REPORT_DETAIL).withString("id", xxReportId).navigation();
    }

}